package com.test.rabbitmq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author : caili
 * @date : 17:14 2023/1/14
 * 广播模式下 同一交换机就能收到消息推送 适合一个操作引起多个服务调用的场景
 */
@Component
@Slf4j
public class FanoutReceiver {
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(),//创建临时队列 队列名称随机生成
            exchange = @Exchange(value = "fanoutExchange", type = "fanout")  //绑定的交换机
    )}, ackMode = "MANUAL")
    public void process(String pMessage, Message message, Channel channel) throws IOException {
        log.info("[广播模式]消费者1接收生产中消息:{}", pMessage);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //设置手动应答 方便演示不做业务逻辑和异常处理了
        channel.basicAck(deliveryTag, false);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(),//创建临时队列
            exchange = @Exchange(value = "fanoutExchange", type = "fanout")  //绑定的交换机
    )}, ackMode = "MANUAL")
    public void process2(String pMessage, Message message, Channel channel) throws IOException {
        log.info("[广播模式]消费者2接收生产中消息:{}", pMessage);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //设置手动应答 方便演示不做业务逻辑和异常处理了
        channel.basicAck(deliveryTag, false);
    }
}
